﻿Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Net.NetworkInformation

Public Class Form1

    Private WithEvents myChat As New TCPChat
    Private myAdapterName, myPhysicalAddress, myGateway, myDNS, strHostName As String
    Private addr() As IPAddress

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If myChat.isConnected Then myChat.disconnect()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '// Getting Ip address of local machine.
        '// First get the host name of local machine.
        strHostName = Dns.GetHostName()
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(strHostName)
        addr = ipEntry.AddressList
        Dim i As Integer
        For i = 0 To addr.Length - 1

            If addr(i).AddressFamily = AddressFamily.InterNetwork Then
                StatusLabel_adapter.Text = "host " & strHostName & _
                                           String.Format(" IP: {0}", addr(i).ToString)
                Exit For
            End If
        Next

        tbx_remoteIP.Text = "127.0.0.1"
        tbx_remotePort.Text = "5000"
        tbx_hostIP.Text = addr(i).ToString
        tbx_hostPort.Text = "5000"
        tbx_remoteIP.Text = addr(i).ToString
    End Sub

    Private Sub btn_Connect_Click(sender As Object, e As EventArgs) Handles btn_Connect.Click
        myChat.connect(tbx_hostIP.Text, CInt(tbx_hostPort.Text))
    End Sub

    Private Sub btn_Disconnect_Click(sender As Object, e As EventArgs) Handles btn_Disconnect.Click
        myChat.disconnect()
    End Sub

    Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
        lbout.Items.Clear()
    End Sub

    Private Sub connection(ByVal status As Boolean) Handles myChat.connection
        btn_Connect.Enabled = Not status
        btn_Disconnect.Enabled = status
        tbx_remoteIP.Enabled = Not status
        tbx_remotePort.Enabled = Not status
        tbx_hostIP.Enabled = Not status
        tbx_hostPort.Enabled = Not status
        tbin.Enabled = status

        If status Then
            StatusLabel_adapter.Image = My.Resources.ledCornerGreen
            StatusLabel_receive.Image = My.Resources.ledCornerOrange
        Else
            StatusLabel_adapter.Image = My.Resources.ledCornerGray
            StatusLabel_receive.Image = My.Resources.ledCornerGray
            StatusLabel_send.Image = My.Resources.ledCornerGray
        End If
    End Sub

    Private Sub myChat_Datareceived(txt As String) Handles myChat.Datareceived
        lbout.Items.Add(Now & " >>In >> " & txt)
    End Sub

    Private Sub rdata(ByVal receiveStatus As Boolean) Handles myChat.recOK
        If receiveStatus Then
            StatusLabel_receive.Image = My.Resources.ledCornerGreen
            StatusLabel_adapter.Text = "local " & myChat.Local.ToString & _
                                       " remote" & myChat.Remote.ToString

        Else
            StatusLabel_receive.Image = My.Resources.ledCornerRed
        End If
    End Sub

    Private Sub sendata(ByVal sendStatus As Boolean) Handles myChat.sendOK
        If sendStatus Then
            StatusLabel_send.Image = My.Resources.ledCornerGreen
        Else
            StatusLabel_send.Image = My.Resources.ledCornerRed
        End If
    End Sub

    Private Sub tbinKD(ByVal sender As Object, _
                       ByVal e As KeyEventArgs) Handles tbin.KeyDown
        If e.KeyCode = Keys.Enter Then
            With CType(sender, TextBox)
                If .Text.Length > 0 Then
                    StatusLabel_send.Image = My.Resources.ledCornerGray
                    myChat.SendData(.Text, tbx_remoteIP.Text, CInt(tbx_remotePort.Text))
                    txtOutSend(.Text)
                    .Text = String.Empty
                End If
            End With
        End If
    End Sub

    Private Sub txtOutSend(ByVal txt As String)
        lbout.Items.Add(Now & " <<Out<< " & txt)
    End Sub
End Class
